import { createI18n } from 'vue-i18n'
import zhcn from './zh-cn.json'
import zhtw from './zh-tw.json'
import en from './en.json'
import pl from './pl.json'
import vi from './vi.json'

// 获取浏览器界面语言，默认语言
//let currentLanguage = navigator.language.replace(/-(\S*)/, '')

let currentLanguage = navigator.language.toLowerCase()
console.log('currlangnav:'+currentLanguage);

if(currentLanguage.indexOf('pl') >= 0||currentLanguage.indexOf('vi') >= 0){
  
}else if(currentLanguage.indexOf('tw') >= 0){
  currentLanguage='zh-tw'

}else if(currentLanguage.indexOf('zh') >=0||currentLanguage.indexOf('cn')){
  currentLanguage='zh-cn'
}else{
    currentLanguage='en'
}
console.log('currlang:'+currentLanguage);
// 如果本地缓存记录了语言环境，则使用本地缓存
let lsLocale = '';
if(localStorage.getItem('baseStore')){
  let baseStore=JSON.parse(localStorage.getItem('baseStore') as string);
  console.log('baseStore:'+baseStore);
  lsLocale=baseStore?.locale
}
console.log(lsLocale);
if (lsLocale!='') {
  currentLanguage =lsLocale
}
console.log(currentLanguage);
export default createI18n({
  locale: currentLanguage,
  legacy: false, // 修复组件引入i18n时vite脚手架报错的问题
  globalInjection: true, // 全局注册 $t
  messages: {
    "zh-cn":zhcn,
    "zh-tw":zhtw,
    en,
    pl,
    vi
  }
})

export const langs = [
  { value: 'zh-cn', text: '中文' },
  { value: 'zh-tw', text: '繁體中文' },
  { value: 'en', text: 'English' },
  { value: 'vi', text: 'Tiếng Việt' },
  { value: 'pl', text: 'Polsk' },
]